package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService {


    @Autowired
    private RegionRepository regionRepository;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void insert(Region region) {
        regionRepository.insert(region);
    }

    @Override
    public void delete(String id) {
        regionRepository.deleteById(id);
    }

    @Override
    public void update(Region region) {
        regionRepository.save(region);
    }

    @Override
    public Region get(String id) {
        //orElse方法从optional中取值，如果取不到返回null
        return regionRepository.findById(id).orElse(null);
    }

    @Override
    public List<Region> list() {
        return regionRepository.findAll();
    }

    @Override
    public Page<Region> query(RegionQuery qo) {
//        //原始代码
//        Query query = new Query();
//        long totalcount = mongoTemplate.count(query, Region.class);
//        if(totalcount == 0){
//            //没有数据
//            return Page.empty();
//        }
//        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
//        query.with(pageable);
//        List<Region> list = mongoTemplate.find(query, Region.class);
//        Page<Region> page =  new PageImpl<>(list, pageable, totalcount);
//        return page;
        return DBHelper.query(mongoTemplate, Region.class, qo, null);
    }

    @Override
    public void saveOrUpdate(Region region) {
        System.out.println(region.getId());
        if(!StringUtils.hasLength(region.getId())){
            //防止id为空串，直接写入到数据库，导致自动生成_id不成功
            region.setId(null);
            regionRepository.save(region);
        }
        else{
            regionRepository.save(region);
        }
    }

    @Override
    public void changeHotValue(String id, Integer hot) {
        //能实现功能，但是需要发送两次数据库请求
//        Region region = regionRepository.findById(id).orElse(null);
//        region.setIshot(hot);
//        regionRepository.save(region);

        //方案二
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("ishot", hot);
        mongoTemplate.updateMulti(query,update,Region.class);

    }

    @Override
    public List<Region> hotRegion() {
        return regionRepository.findByIshotOrderBySequence(Region.STATE_HOT);
    }
}
